package com.fingard.dsp.bank.directbank.yqb01.util;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JsonUtil {
    public static ObjectMapper mapper;

    static {
        JsonUtil.mapper = new ObjectMapper();
    }

    /**
     * 将content转为java object
     *
     * @param <T>
     * @param pContent
     * @param pObj
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static <T> Object readValue(String pContent, Object pObj)
            throws JsonParseException, JsonMappingException, IOException {
        return JsonUtil.mapper.readValue(pContent, (Class<T>) pObj);
    }

    /**
     * 将object转为json字符串
     *
     * @param pObj
     * @return
     * @throws IOException
     * @throws JsonMappingException
     * @throws JsonGenerationException
     */
    public static String writeValue(Object pObj) throws JsonGenerationException, JsonMappingException, IOException {
        return JsonUtil.mapper.writeValueAsString(pObj);
    }
}
